# FlorisBoard to CocoKeyboard
$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $projectRoot

Write-Host "Starting replacement..." -ForegroundColor Cyan

# Simple replacement list (order matters!)
$rules = @(
    # Package names first
    'dev.patrickgold.florisboard', 'com.coco.cocokeyboard'
    'dev/patrickgold/florisboard', 'com/coco/cocokeyboard'
    'dev\patrickgold\florisboard', 'com\coco\cocokeyboard'
    
    # Class names
    'FlorisApplication', 'CocoApplication'
    'FlorisImeService', 'CocoImeService'
    'FlorisAppActivity', 'CocoAppActivity'
    'FlorisPreferenceModel', 'CocoPreferenceModel'
    'FlorisPreferenceStore', 'CocoPreferenceStore'
    'FlorisPreferenceData', 'CocoPreferenceData'
    'FlorisScreen', 'CocoScreen'
    'FlorisEmojiCompat', 'CocoEmojiCompat'
    'FlorisRef', 'CocoRef'
    'FlorisLocale', 'CocoLocale'
    'FlorisRect', 'CocoRect'
    'FlorisBoard', 'CocoKeyboard'
    
    # Uppercase
    'FLORISBOARD', 'COCOKEYBOARD'
    'FLADDONS', 'COCOADDONS'
    
    # Lowercase with underscore
    'floris_app', 'coco_app'
    'floris_', 'coco_'
    
    # Simple lowercase
    'florisboard', 'cocokeyboard'
    
    # URLs
    'florisboard.org', 'cocokeyboard.com'
    '/florisboard/', '/cocokeyboard/'
)

# File extensions
$extensions = '*.kt', '*.kts', '*.xml', '*.gradle', '*.json', '*.md', '*.txt', '*.toml', '*.pro', '*.properties'

# Exclude folders
$exclude = '.git', '.gradle', '.idea', '.kotlin', 'build'

Write-Host "Scanning files..." -ForegroundColor Yellow

$files = Get-ChildItem -Path $projectRoot -Include $extensions -Recurse -File -ErrorAction SilentlyContinue |
    Where-Object {
        $path = $_.FullName
        $ok = $true
        foreach ($dir in $exclude) {
            if ($path -like "*\$dir\*") {
                $ok = $false
                break
            }
        }
        $ok
    }

Write-Host "Found $($files.Count) files" -ForegroundColor Green
Write-Host ""

$modified = 0
$count = 0

foreach ($file in $files) {
    $count++
    if ($count % 100 -eq 0) {
        Write-Host "Progress: $count / $($files.Count)" -ForegroundColor DarkGray
    }
    
    try {
        $content = Get-Content -Path $file.FullName -Raw -Encoding UTF8
        $original = $content
        
        # Apply all replacements
        for ($i = 0; $i -lt $rules.Count; $i += 2) {
            $old = $rules[$i]
            $new = $rules[$i + 1]
            $content = $content.Replace($old, $new)
        }
        
        if ($content -ne $original) {
            Set-Content -Path $file.FullName -Value $content -Encoding UTF8 -NoNewline
            $modified++
            $name = $file.Name
            Write-Host "  OK: $name" -ForegroundColor Gray
        }
    }
    catch {
        Write-Host "  ERROR: $($file.Name)" -ForegroundColor Red
    }
}

Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "DONE!" -ForegroundColor Green
Write-Host "Modified $modified files" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host "1. Run: ./gradlew clean" -ForegroundColor White
Write-Host "2. Sync project in Android Studio" -ForegroundColor White
Write-Host ""

